import RFIDReader from "./RFIDReader";
class ReaderFunction{
  
    /**
     *  打开设备
     */
    OpenDevice(device,callback){
        if(RFIDReader.device_id == null){
            RFIDReader.Sys_Open(device,function(result){
                if(result !==null && result.code ==1){
                    RFIDReader.device_id = result.data.device_id;
                }
                callback(result);
            });
        }else{
            let result = {};
            result.code = 1;
            result.retval = 0;
            result.data = {};
            result.data.device_id = RFIDReader.device_id;
            callback(result);
        }
    }
    /**
     * 字符串转16进制的ASCII码
     */
    stringToHex(str){
        let val = "";
        for(let i = 0; i< str.length;i++){
            val += str.charCodeAt(i).toString(16);
        }
        return val;
    }
    /**
     *  16进制数的ASCII码转字符串
     */
    hexToString(hex){
        let val = "";
        for(let i = 0;i<hex.length;i+=2){
            val += String.fromCharCode(parseInt(hex.substring(i, i+2), 16));
        }
        return val;
    }
    /*
		*获取卡号（UID）
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*
		*返回数据:
		*成功：result.data.snr:卡号 
		*出错：见RFIDReader.js说明
	*/
    TyA_GetUID(device,callback){
        this.OpenDevice(device,function(result){
            if(result == null || result.code != 1 || result.retval != 0){
                callback(result);
                return;
            }
            RFIDReader.Sys_InitType(RFIDReader.device_id,'A',function(result){
                if(result == null || result.code != 1 || result.retval != 0){
                    callback(result);
                    return;
                }
                RFIDReader.Sys_SetAntenna(RFIDReader.device_id,1,function(result){
                    if(result == null || result.code != 1 || result.retval != 0){
                        callback(result);
                        return ;
                    }
                    RFIDReader.TyA_Request(RFIDReader.device_id,0x52,function(result){
                        if(result == null || result.code != 1 || result.retval != 0){
                            callback(result);
                            return;
                        }
                        RFIDReader.TyA_Anticollision(RFIDReader.device_id,callback);
                    })
                })
            })
        })
    }
    /*
		*获取8字节卡号（UID）
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*
		*返回数据:
		*成功：result.data.snr:卡号 
		*出错：见RFIDReader.js说明
	*/
    TyA_GetUID2(device,callback){
        this.OpenDevice(device, function(result)
			{
				if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
				RFIDReader.Sys_InitType(RFIDReader.device_id, 'A', function(result){
					if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
					RFIDReader.Sys_SetAntenna(RFIDReader.device_id, 1, function(result){
						if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						}
						RFIDReader.TyA_Request(RFIDReader.device_id, 0x52, function(result){
							if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
							RFIDReader.TyA_NTAG_AnticollSelect(RFIDReader.device_id, callback);
						});
					});
				});
			});
    }
    /*
		*读取Mifare卡扇区数据
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*block: 块号
		*key: 块号所在扇区的密钥
		*
		*返回数据:
		*成功：
		*	result.data.snr:卡号
		*	result.data.value:扇区数据
		*出错：见RFIDReader.js说明
	*/
    TyA_CS_Read2(device,block,key,callback){
        this.OpenDevice(device, function(result)
			{
				if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
				RFIDReader.Sys_InitType(RFIDReader.device_id, 'A', function(result){
					if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
					RFIDReader.Sys_SetAntenna(RFIDReader.device_id, 1, function(result){
						if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						}
						RFIDReader.TyA_Request(RFIDReader.device_id, 0x52, function(result){
							if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
							RFIDReader.TyA_Anticollision(RFIDReader.device_id, function(result){
								if(result == null || result.code != 1 || result.retval != 0)
								{
									callback(result);
									return;
								}
								RFIDReader.TyA_Select(RFIDReader.device_id, result.data.snr, function(result){
									if(result == null || result.code != 1 || result.retval != 0)
									{
										callback(result);
										return;
									}
									RFIDReader.TyA_CS_Authentication2(RFIDReader.device_id, 0x60, block, key, function(result){
										if(result == null || result.code != 1 || result.retval != 0)
										{
											callback(result);
											return;
										}
										RFIDReader.TyA_CS_Read(RFIDReader.device_id, block, callback);
									});
								});
							});
						});
					});
				});
			});
    }
    /*
		*写Mifare卡扇区数据
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*block: 块号
		*key: 块号所在扇区的密钥
		*data：扇区数据，16字节
		*
		*返回数据:
		*成功：
		*
		*出错：见RFIDReader.js说明
	*/
    TyA_CS_Write2(device,block,key,data,callback){
        this.OpenDevice(device, function(result)
			{
				if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
				RFIDReader.Sys_InitType(RFIDReader.device_id, 'A', function(result){
					if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
					RFIDReader.Sys_SetAntenna(RFIDReader.device_id, 1, function(result){
						if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						}
						RFIDReader.TyA_Request(RFIDReader.device_id, 0x52, function(result){
							if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
							RFIDReader.TyA_Anticollision(RFIDReader.device_id, function(result){
								if(result == null || result.code != 1 || result.retval != 0)
								{
									callback(result);
									return;
								}
								RFIDReader.TyA_Select(RFIDReader.device_id, result.data.snr, function(result){
									if(result == null || result.code != 1 || result.retval != 0)
									{
										callback(result);
										return;
									}
									RFIDReader.TyA_CS_Authentication2(RFIDReader.device_id, 0x60, block, key, function(result){
										if(result == null || result.code != 1 || result.retval != 0)
										{
											callback(result);
											return;
										}
										RFIDReader.TyA_CS_Write(RFIDReader.device_id, block, data, callback);
									});
								});
							});
						});
					});
				});
			});
    }
    /*
		*读取NTag卡扇区数据
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*addr: 页地址
		*key: 卡片密钥
		*
		*返回数据:
		*成功：
		*	result.data.value:4页（16字节）数据
		*出错：见RFIDReader.js说明
	*/
    TyA_NTAG_Read2(device,addr,key,callback){
        this.OpenDevice(device, function(result)
			{
				if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
				RFIDReader.Sys_InitType(RFIDReader.device_id, 'A', function(result){
					if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
					RFIDReader.Sys_SetAntenna(RFIDReader.device_id, 1, function(result){
						if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						}
						RFIDReader.TyA_Request(RFIDReader.device_id, 0x52, function(result){
							if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
							RFIDReader.TyA_NTAG_AnticollSelect(RFIDReader.device_id, function(result){
								if(result == null || result.code != 1 || result.retval != 0)
								{
									callback(result);
									return;
								}

								RFIDReader.TyA_NTAG_PwdAuth(RFIDReader.device_id, key, function(result){
									if(result == null || result.code != 1 || result.retval != 0)
									{
										callback(result);
										return;
									}
									RFIDReader.TyA_NTAG_Read(RFIDReader.device_id, addr, callback);
								});
							});
							
						});
					});
				});
		});
    }
    /*
		*向NTag卡写入1页数据
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*addr: 页地址
		*key: 卡片密钥
		*data：扇区数据，4字节
		*
		*返回数据:
		*成功：
		*
		*出错：见RFIDReader.js说明
	*/
    TyA_NTAG_Write2(device,addr,key,data,callback){
        this.OpenDevice(device,function(result){
            if(result == null || result.code != 1 || result.retval != 0){
                callback(result);
				return;
            }
            RFIDReader.Sys_InitType(RFIDReader.device_id,'A',function(result){
                if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
                RFIDReader.Sys_SetAntenna(RFIDReader.device_id,1,function(result){
                    if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
                    RFIDReader.TyA_Request(RFIDReader.device_id,0x52,function(result){
                        if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						};
                        RFIDReader.TyA_NTAG_AnticollSelect(RFIDReader.device_id,function(result){
                            if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
                            RFIDReader.TyA_NTAG_PwdAuth(RFIDReader.device_id,key,function(result){
                                if(result == null || result.code != 1 || result.retval != 0)
								{
									callback(result);
									return;
								}
								RFIDReader.TyA_NTAG_Write(RFIDReader.device_id, addr, data, callback);
                            })
                        })
                    })
                })
            })
        });
    }
    /*
		*向NTag卡写入4页数据
		*
		*参数：
		*device：设备号，只连接一台设备的情况下填0即可
		*addr: 页地址
		*key: 卡片密钥
		*data：页数据，16字节
		*
		*返回数据:
		*成功：
		*
		*出错：见RFIDReader.js说明
	*/
    TyA_NTAG_CompWrite2(device,addr,key,data,callback){
        this.OpenDevice(device,function(result){
            if(result == null || result.code != 1 || result.retval != 0){
                callback(result);
                return;
            }
            RFIDReader.Sys_InitType(RFIDReader.device_id,'A',function(result){
                if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
                RFIDReader.Sys_SetAntenna(RFIDReader.device_id,1,function(result){
                    if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
                    RFIDReader.TyA_Request(RFIDReader.device_id,0x52,function(result){
                        if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						}
                        RFIDReader.TyA_NTAG_AnticollSelect(RFIDReader.device_id,function(result){
                            if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
                            RFIDReader.TyA_NTAG_PwdAuth(RFIDReader.device_id,key,function(result){
                                if(result == null || result.code != 1 || result.retval != 0)
								{
									callback(result);
									return;
								}
                                RFIDReader.TyA_NTAG_Write(RFIDReader.device_id,addr,data,callback);
                            })
                        })
                    })
                })
            })
        })
    }

    /*
		*向UID卡0扇区0块写入数据，在写入的数据Byte[0] ~ Byte[15]中，Byte[4]必须是Byte[0] ~ Byte[3]异或运算的结果，函数内部会自动修改Byte[4]的值
		*
		*参数：
		*device_id: 设备ID
		*
		*data: 写入的数据，16字节
		*
		*callback:回调函数
		*
		*返回数据:
		*无
		*
		*出错：见RFIDReader.js说明
	*/
    TyA_UID_Write2(device,key,data,callback){
        this.OpenDevice(device,function(result){
            if(result == null || result.code !=1 || result.retval != 0){
                callback(result);
                return;
            }
            RFIDReader.Sys_InitType(RFIDReader.device_id,'A',function(result){
                if(result == null || result.code != 1 || result.retval != 0)
				{
					callback(result);
					return;
				}
                RFIDReader.Sys_SetAntenna(RFIDReader.device_id,1,function(result){
                    if(result == null || result.code != 1 || result.retval != 0)
					{
						callback(result);
						return;
					}
                    RFIDReader.TyA_Request(RFIDReader.device_id,0x52,function(result){
                        if(result == null || result.code != 1 || result.retval != 0)
						{
							callback(result);
							return;
						}
                        RFIDReader.TyA_Anticollision(RFIDReader.device_id,function(result){
                            if(result == null || result.code != 1 || result.retval != 0)
							{
								callback(result);
								return;
							}
                            RFIDReader.TyA_Select(RFIDReader.device_id,result.data.snr,function(result){
                                if(result == null || result.code != 1 || result.retval != 0)
								{
									callback(result);
									return;
								}
                                RFIDReader.TyA_UID_Write(RFIDReader.device_id,data,callback);
                            });
                        });
                    });
                });
            });
        });
    }
}

export  default new ReaderFunction();